package tests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Scanner;

import main.Conference;
import main.Manuscript;
import main.User;

import org.junit.Before;
import org.junit.Test;


import junit.framework.TestCase;

/**
 * Tests the reading of a config test file, specifically the methods
 * loadConferences and loadUsers in the class StartFrame
 * 
 * @author Cristopher Claeys
 * @version 12/3/11
 */
public class ConfigTest extends TestCase{


	ArrayList<Conference> my_confs;
	ArrayList<String> read_dirs;
	String directory;

	/**
	 * Sets up data from a test config file
	 */
	@Before
	public void setUp() throws FileNotFoundException {
		my_confs = new ArrayList<Conference>();
		read_dirs = new ArrayList<String>();
		int counter = 0;
		GregorianCalendar dead1 = new GregorianCalendar(2011,12,5);
		GregorianCalendar dead2 = new GregorianCalendar(2012,3,5);
		//begin code to be tested
		File config = new File("configTEST.txt");
		Scanner scan = new Scanner(new FileReader(config));
		directory = scan.nextLine();
		while(scan.hasNextLine()){
			//method called in next line tested in SerialTest
			//Conference temp = restoreConf(new File(directory+scan.nextLine()+".ser"));
			
			//replacement code for above:
			Conference temp = new Conference("conf"+counter, dead1, dead2, 
					new ArrayList<Manuscript>(), new ArrayList<User>());//create unique conferences
			//add unique users
			temp.addUser(new User(""+counter, ""+counter));
			//end replacement
			
			//test replaced scanning
			read_dirs.add(directory+scan.nextLine()+".ser");
			temp.setDirectory(directory);
			my_confs.add(temp);
			counter++;
		}//end tested code
		
		//add some redundant users
		my_confs.get(0).addUser(new User("1", "1"));
		my_confs.get(2).addUser(new User("0", "0"));
		my_confs.get(2).addUser(new User("1", "1"));
	}//end setUp


	/**
	 * Tests the code associated with loading data from the config file
	 */
	@Test
	public void testConfig() {
		assertEquals("directory not expected", "directory/folder/", directory);
		ArrayList<String> conf_addresses = new ArrayList<String>();
		//expected conf addresses as seen in configTest.txt
		conf_addresses.add("directory/folder/conf0.ser");
		conf_addresses.add("directory/folder/conf1.ser");
		conf_addresses.add("directory/folder/conf3.ser");
		for(int i = 0; i<my_confs.size(); i++){
			assertEquals("read address not matching expected", conf_addresses.get(i),
					read_dirs.get(i));
		}
		
		//begin code to be tested
		ArrayList<User> the_users = new ArrayList<User>();
		for(int i = 0; i < my_confs.size(); i++) {
			for(int j = 0; j < my_confs.get(i).getUsers().size(); j++){
				if(!the_users.contains(my_confs.get(i).getUsers().get(j))){
					the_users.add(my_confs.get(i).getUsers().get(j));
				}
			}
		}//end code to be tested
		
		assertEquals("users not properly collected from confs", the_users.size(), 3);
	}//end testConfig
}
